﻿namespace YidanSoft.Library.EditorUtility.Collections
{
    using System;
    using System.Collections;
    using System.Reflection;
    using System.Threading;
    using System.Windows.Forms;

    public class RebarBandCollection : IEnumerable
    {
        private ArrayList bands = new ArrayList();
        private EventHandler Changed;

        public event EventHandler Changed
        {
            add
            {
                EventHandler handler2;
                EventHandler changed = this.Changed;
                do
                {
                    handler2 = changed;
                    EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
                    changed = Interlocked.CompareExchange<EventHandler>(ref this.Changed, handler3, handler2);
                }
                while (changed != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler changed = this.Changed;
                do
                {
                    handler2 = changed;
                    EventHandler handler3 = (EventHandler) Delegate.Remove(handler2, value);
                    changed = Interlocked.CompareExchange<EventHandler>(ref this.Changed, handler3, handler2);
                }
                while (changed != handler2);
            }
        }

        public int Add(Control control)
        {
            if (this.Contains(control))
            {
                return -1;
            }
            int num = this.bands.Add(control);
            this.RaiseChanged();
            return num;
        }

        public void Clear()
        {
            while (this.Count > 0)
            {
                this.RemoveAt(0);
            }
        }

        public bool Contains(Control control)
        {
            return this.bands.Contains(control);
        }

        public IEnumerator GetEnumerator()
        {
            return this.bands.GetEnumerator();
        }

        public int IndexOf(Control control)
        {
            return this.bands.IndexOf(control);
        }

        private void RaiseChanged()
        {
            if (this.Changed != null)
            {
                this.Changed(this, null);
            }
        }

        public void Remove(Control control)
        {
            this.bands.Remove(control);
            this.RaiseChanged();
        }

        public void RemoveAt(int index)
        {
            this.bands.RemoveAt(index);
            this.RaiseChanged();
        }

        public int Count
        {
            get
            {
                return this.bands.Count;
            }
        }

        public Control this[int index]
        {
            get
            {
                return (Control) this.bands[index];
            }
        }
    }
}

